/*
 * readCount.scala
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package solidutils


class readCount(position:Int,readLength:Int) {

    //count-variables for the different posibilities
    val A:Array[Int]= new Array[Int](2*readLength)
    val T:Array[Int]= new Array[Int](2*readLength)
    val G:Array[Int]= new Array[Int](2*readLength)
    val C:Array[Int]= new Array[Int](2*readLength)
    val N:Array[Int]= new Array[Int](2*readLength)
    val a:Array[Int]= new Array[Int](2*readLength)
    val t:Array[Int]= new Array[Int](2*readLength)
    val g:Array[Int]= new Array[Int](2*readLength)
    val c:Array[Int]= new Array[Int](2*readLength)
    val n:Array[Int]= new Array[Int](2*readLength)
    var senseCoverage:Int=0
    var antisenseCoverage=0

    def count(seq:String,sense:Boolean):Unit ={
        if(seq.length!=readLength){
            throw new Exception("read with a length of "+seq.length+", at position "+position+"\n"+seq)
        }
        val base:Int=if (sense) 0 else readLength
        for (i <- 0 to readLength-1){
            seq.charAt(i) match{
                case 'A' => A(i+base)+= 1
                case 'T' => T(i+base)+= 1
                case 'G' => G(i+base)+= 1
                case 'C' => C(i+base)+= 1
                case 'N' => N(i+base)+= 1
                case 'a' => a(i+base)+= 1
                case 't' => t(i+base)+= 1
                case 'g' => g(i+base)+= 1
                case 'c' => c(i+base)+= 1
                case 'n' => n(i+base)+= 1
                case _ => throw new Exception("read at position "+position+"with unknown nucleotide: "+seq.charAt(i))
            }
        }
        if(sense){
            senseCoverage+= 1
        }else{
            antisenseCoverage+= 1
        }
    }

    def getPosition():Int=position

    """returns the count for nucleotide nuc at position pos given the strand."""
    def get(pos:Int,sense:Boolean,nuc:Char):Int= {
        if (!this.covers(pos)){
            throw new Exception("Position "+pos+" is not covered by a read at position "+position)
        }
        val i= if (sense) pos-position else pos-position+readLength
        nuc match{
            case 'A' => A(i)
            case 'T' => T(i)
            case 'G' => G(i)
            case 'C' => C(i)
            case 'N' => N(i)
            case 'a' => a(i)
            case 't' => t(i)
            case 'g' => g(i)
            case 'c' => c(i)
            case 'n' => n(i)
            case _ => throw new Exception("asked for unknown nucleotide: "+nuc)
        }
    }

    def getSenseCoverage():Int= senseCoverage

    def getAntisenseCoverage():Int= antisenseCoverage

    def covers(pos:Int):Boolean= pos>=position && pos<position+readLength

    def completelyDownstream(pos:Int):Boolean= position+readLength-1<pos
}
